using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Ent.SqlDB;
using System.Data;
namespace Ent.Voice.Nomination
{
    public enum VoteResponse { Success, VerificationPending, EventVoted, EntryAttempted, EventVotedEntryAttempted, NoVote }
    public enum Option { None, Vote, Point }
    public class Vote
    {
        public int VoteId = -1;
        public string Name = "no-name";
        public string Email = "";
        public string Comment = "";
        public Option Option;
        public int Points = 0;
        public string IP = "";
        public string HostName = "";
        public int EntryId = 0;
        public DateTime VoteDate = DateTime.Now;
        public bool Verified = false;
        public static Option GetOption(object opt)
        {
            switch (opt.ToString().ToLower())
            {
                case "1":
                case "none":
                    return Option.None;
                case "2":
                case "vote":
                    return Option.Vote;
                case "3":
                case "point":
                default:
                    return Option.Point;
            }
        }
        public static VoteResponse GetFeedCheck(string res)
        {
            switch (res.ToLower())
            {
                case "eventvoted|entryattempted":
                    return VoteResponse.EventVotedEntryAttempted;
                case "entryattempted":
                    return VoteResponse.EntryAttempted;
                case "eventvoted":
                    return VoteResponse.EventVoted;
                case "verificationpending":
                    return VoteResponse.VerificationPending;
                case "success":
                    return VoteResponse.Success;
                case "novote":
                default:
                    return VoteResponse.NoVote;
            }
        }
        private VoteResponse DoFeedCheck()
        {
            Dictionary<string, object> pars = new Dictionary<string, object>();
            pars.Add("Entry", this.EntryId);
            pars.Add("IP", this.IP);
            pars.Add("Host", this.HostName);
            pars.Add("Email", this.Email);
            Dictionary<string, VSqlDbType> outPars = new Dictionary<string, VSqlDbType>();
            outPars.Add("Result", new VSqlDbType(System.Data.SqlDbType.VarChar, 25));
            ReaderResponse rr = Execute.SPReader("Voice_DoFeedCheck", pars, outPars);
            return GetFeedCheck(rr.OutParams["Result"].ToString());
        }
        public VoteResponse AddVote(bool doWarn)
        {
            VoteResponse feedCheck = DoFeedCheck();
            if (doWarn && this.Option == Option.Vote && (feedCheck == VoteResponse.EventVoted || feedCheck == VoteResponse.EventVotedEntryAttempted))
                return VoteResponse.EventVoted;
            if (doWarn && (feedCheck == VoteResponse.EntryAttempted || feedCheck==VoteResponse.EventVotedEntryAttempted))
                return VoteResponse.EntryAttempted;
            
            Dictionary<string, object> pars = new Dictionary<string, object>();
            pars.Add("Entry", this.EntryId);
            pars.Add("IP", this.IP);
            pars.Add("Host", this.HostName);
            pars.Add("Email", this.Email);
            pars.Add("Name", this.Name);
            pars.Add("Comment", this.Comment);
            pars.Add("Option", this.Option.ToString());
            pars.Add("Points", this.Points);
            Dictionary<string, VSqlDbType> outPars = new Dictionary<string, VSqlDbType>();
            outPars.Add("VoteId", new VSqlDbType(System.Data.SqlDbType.Int));
            outPars.Add("Result", new VSqlDbType(System.Data.SqlDbType.VarChar, 25));
            ReaderResponse rr = Execute.SPReader("Voice_AddFeed", pars, outPars);
            this.VoteId = (int)rr.OutParams["VoteId"];
            return GetFeedCheck(rr.OutParams["Result"].ToString());
        }
        public static ActivationResponse Activate(string voteId, string ip)
        {
            Dictionary<string, object> pars = new Dictionary<string, object>();
            pars.Add("VoteId", voteId);
            pars.Add("IP", ip);
            Dictionary<string, VSqlDbType> outpars = new Dictionary<string, VSqlDbType>();
            outpars.Add("Result", new VSqlDbType(SqlDbType.VarChar, 20));
            NonQueryResponse nqr = Execute.SPNonQuery("Voice_ActivateFeed", pars, outpars);
            string res = nqr.OutParams["Result"].ToString();
            switch (res.ToLower())
            {
                case "activated":
                    return ActivationResponse.Activated;
                case "invalidactivation":
                    return ActivationResponse.InvalidActivation;
                default:
                    return ActivationResponse.Error;
            }
        }
        public static Vote[] GetNominationVotes(int EntryId, int startIndex, int count, ref int total)
        {
            Dictionary<string, object> pars = new Dictionary<string, object>();
            pars.Add("EntryId", EntryId);
            pars.Add("startIndex", startIndex);
            pars.Add("rowCount", count);
            pars.Add("orderBy", "Dated");
            Dictionary<string, VSqlDbType> outpars = new Dictionary<string, VSqlDbType>();
            outpars.Add("count", new VSqlDbType(SqlDbType.Int, -1));
            ReaderResponse rr = Execute.SPReader("Voice_GetNominationVotes", pars, outpars);
            total = Convert.ToInt32(rr.OutParams["count"]);
            if (rr.Table != null && rr.Table.Rows.Count > 0)
            {
                Vote[] retValue = new Vote[rr.Table.Rows.Count];
                for (int i = 0; i < rr.Table.Rows.Count; i++)
                {
                    retValue.SetValue(SetVote(rr.Table.Rows[i]), i);
                }
                return retValue;
            }
            else
                return null;
        }
        private static Vote SetVote(DataRow dr)
        {
            Vote retValue = new Vote();
            if (dr.Table.Columns.Contains("Id")) retValue.VoteId = Convert.ToInt32(dr["Id"]);
            if (dr.Table.Columns.Contains("Name")) retValue.Name = Convert.ToString(dr["Name"]);
            if (dr.Table.Columns.Contains("Comment")) retValue.Comment = Convert.ToString(dr["Comment"]);
            if (dr.Table.Columns.Contains("Opt")) 
                retValue.Option = dr["Opt"] != System.DBNull.Value ? (Option)Enum.Parse(typeof(Option), Convert.ToString(dr["Opt"]), true) : Option.None;
            if (dr.Table.Columns.Contains("Point")) retValue.Points = Convert.ToInt32(dr["Point"]);
            if (dr.Table.Columns.Contains("Dated")) retValue.VoteDate = Convert.ToDateTime(dr["Dated"]);
            return retValue;
        }
    }
}
